package com.glink.manage.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.BaseCommonUtils;
import com.glink.manage.domain.PersonTrainingRecordBean;
import com.glink.manage.dto.person.PersonTrainingRecordIdDTO;
import com.glink.manage.dto.person.PersonTrainingRecordPageDTO;
import com.glink.manage.dto.person.PersonTrainingRecordSaveDTO;
import com.glink.manage.mapper.PersonTrainingRecordMapper;
import com.glink.manage.service.PersonTrainingRecordService;
import com.glink.manage.vo.person.PersonTrainingRecordVO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author  qiushaoshan
 * @since 2025-01-13
 */
@Service
@Slf4j
public class PersonTrainingRecordServiceImpl extends ServiceImpl<PersonTrainingRecordMapper, PersonTrainingRecordBean> implements PersonTrainingRecordService {

    /**
     * 分页查询用户的培训记录
     *
     * @param dto 用户信息
     * @return 培训记录
     */
    @Override
    public IPage<PersonTrainingRecordVO> pageByTrainingRecord(PersonTrainingRecordPageDTO dto) {
        IPage<PersonTrainingRecordBean> constructionWorkBeanIPage = new Page<>(dto.getPage(), dto.getSize());

        IPage<PersonTrainingRecordBean> workBeanIPage = this.lambdaQuery()
                .eq(PersonTrainingRecordBean::getPersonObjId, dto.getPersonObjId())
                .orderByDesc(PersonTrainingRecordBean::getTime)
                .orderByDesc(PersonTrainingRecordBean::getCreateTime)
                .orderByDesc(PersonTrainingRecordBean::getObjId)
                .page(constructionWorkBeanIPage);

        IPage<PersonTrainingRecordVO> pageVOIPage = new Page<>(workBeanIPage.getCurrent(), workBeanIPage.getSize(), workBeanIPage.getTotal());
        List<PersonTrainingRecordVO> pageVOList = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(workBeanIPage.getRecords())) {
            workBeanIPage.getRecords().forEach(trainingRecordBean -> {
                PersonTrainingRecordVO recordVO = new PersonTrainingRecordVO();
                BeanUtils.copyProperties(trainingRecordBean, recordVO);
                pageVOList.add(recordVO);
            });
            pageVOIPage.setRecords(pageVOList);
        }
        return pageVOIPage;
    }

    /**
     * 保存培训记录
     *
     * @param dto 培训记录
     * @return 保存状态
     */
    @Override
    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean saveTrainingRecord(PersonTrainingRecordSaveDTO dto) throws GeneralSimpleException {
        
        if(StringUtils.isBlank(dto.getPersonObjId())){
            throw new GeneralSimpleException("400039");
        }
        
        PersonTrainingRecordBean trainingRecordBean = new PersonTrainingRecordBean();
        trainingRecordBean.setObjId(BaseCommonUtils.generateUUID());
        trainingRecordBean.setPersonObjId(dto.getPersonObjId());
        trainingRecordBean.setContent(dto.getContent());
        trainingRecordBean.setTime(dto.getTime());
        trainingRecordBean.setProve(dto.getProve());
        return save(trainingRecordBean);
    }

    /**
     * 删除培训记录
     *
     * @param dto 培训记录ID
     * @return 删除状态
     */
    @Override
    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean delTrainingRecord(PersonTrainingRecordIdDTO dto) throws GeneralSimpleException {
        
        PersonTrainingRecordBean trainingRecordBean = this.lambdaQuery()
                .eq(PersonTrainingRecordBean::getObjId, dto.getObjId())
                .one();
        if(Objects.nonNull(trainingRecordBean)){
            Integer count = this.lambdaQuery().eq(PersonTrainingRecordBean::getPersonObjId, trainingRecordBean.getPersonObjId()).count();
            if(Objects.nonNull(count) && count == 1){
                throw new GeneralSimpleException("400071");
            }

            return removeById(dto.getObjId());
        }
        return true;
    }
}
